// 导入db模块
const db = require('../config/db');

// 签到类
class Signin {
    // 添加签到
    add(s_year2, s_month2, s_date2, s_text, user_id) {
        // console.log({s_year, s_date, s_month, s_text, user_id})
        return new Promise((resolve, reject) => {
            // `选择 字段 来自 签到数据表  如果字段等于传递过来的值 就返回符合条件执行结果`
            let sql = `select s_text from db_signin where s_text='${s_text}' and user_id='${user_id}'`
            db.query(sql)
                .then(
                    (rows) => {
                        if (rows.length == 0) {
                            // 表示没有这个签到
                            let sql2 = `insert into db_signin(s_year2, s_date2, s_month2, s_text, user_id)values('${s_year2}','${s_date2}','${s_month2}','${s_text}','${user_id}')`;
                            db.query(sql2).then(
                                () => {
                                    resolve({
                                        code: 200,
                                        msg: `签到添加成功-"${s_text}"。`
                                    })
                                },
                                (err) => {
                                    console.log('-->', err)
                                    reject({
                                        code: -1,
                                        msg: `添加签到失败。`,
                                        err
                                    })
                                }
                            )

                        } else {
                            // 表示已经存在这个签到
                            resolve({
                                code: 1,
                                msg: `今天已经签到。`
                            })
                        }
                    },
                    (err) => {
                        reject({
                            code: -1,
                            msg: '查询签到失败。'
                        })
                    }
                )
        })
    }

    // 获取签到列表
    list(user_id) {
        return new Promise((resolve, reject) => {
            let sql = `select * from db_signin where user_id = '${user_id}'`;
            db.query(sql).then(
                (rows) => {
                    resolve({
                        code: 200,
                        msg: '获取签到列表成功。',
                        result: rows
                    })
                },
                (err) => {
                    reject({
                        code: -1,
                        msg: '获取签到列表失败。',
                        err
                    })
                }
            )
        })
    }

    // 删除签到
    remove(id) {
        return new Promise((resolve, reject) => {
            let sql = `select s_text from db_signin where id = '${id}'`;
            db.query(sql)
                .then(
                    (rows) => {
                        if (rows.length == 0) {
                            resolve({
                                code: 404,
                                msg: '该签到不存在。'
                            })
                        }
                        else {
                            // 删除签到
                            let sql2 = `delete from db_signin where id = '${id}'`;
                            db.query(sql2)
                                .then(
                                    (result) => {
                                        // console.log(result)
                                        if (result.affectedRows === 1) {
                                            resolve({
                                                code: 200,
                                                msg: '删除签到成功。',
                                            })
                                        }
                                        else {
                                            resolve({
                                                code: 200,
                                                msg: '删除签到失败。',
                                            })
                                        }

                                    },
                                    (err) => {
                                        reject({
                                            code: -1,
                                            msg: '删除签到失败。',
                                            err
                                        })
                                    }
                                )
                        }
                    },
                    (err) => {
                        reject({
                            code: -1,
                            msg: '查询签到失败。',
                            err
                        })
                    }
                )
        })
    }

    // 统计个人签到
    total(user_id) {
        return new Promise((resolve, reject) => {
            let sql = `select count(*) as total from db_signin where user_id='${user_id}'`
            db.query(sql)
                .then(
                    (rows) => {
                        resolve({
                            code: 200,
                            msg: '统计签到成功。',
                            result: rows
                        })
                    },
                    (err) => {
                        reject({
                            code: -1,
                            msg: '统计签到失败。',
                            err
                        })
                    }
                )
        })
    }

    // 统计所有签到
    total_all() {
        return new Promise((resolve, reject) => {
            let sql = `select count(*) as total from db_signin`
            db.query(sql)
                .then(
                    (rows) => {
                        resolve({
                            code: 200,
                            msg: '统计签到成功。',
                            result: rows
                        })
                    },
                    (err) => {
                        reject({
                            code: -1,
                            msg: '统计签到失败。',
                            err
                        })
                    }
                )
        })
    }

}

module.exports = new Signin();